package es.udc.emarketapp.http.controller.actions.orders;

import es.udc.emarketapp.emarket.model.purchase.purchaseFacade.PurchaseFacade;
import es.udc.emarketapp.emarket.model.purchase.purchaseFacade.vo.OrderResumeChunkVO;
import es.udc.emarketapp.http.view.actionForms.FindOrdersByDateForm;
import es.udc.fbellas.j2ee.util.exceptions.InternalErrorException;
import es.udc.fbellas.j2ee.util.struts.action.DefaultAction;

import java.io.IOException;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class FindOrdersByDateAction1 extends DefaultAction {

    public ActionForward doExecute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException, InternalErrorException {


        /* Get data. */
        FindOrdersByDateForm findForm = (FindOrdersByDateForm) form;

        Calendar startDate = findForm.getStartDate();
        Calendar endDate = findForm.getEndDate();
        int startIndex = findForm.getStartIndex();
        int count = findForm.getCount();

        return doFindOrdersByDate(mapping, startDate, endDate,
                startIndex, count, request);
    }


    private ActionForward doFindOrdersByDate(ActionMapping mapping, Calendar startDate, Calendar endDate,
            int startIndex, int count, HttpServletRequest request) throws InternalErrorException {

        System.out.println("startDate "+ startDate + "endDate" + endDate + "startIndex " + startIndex);


        PurchaseFacade purchaseFacade = new PurchaseFacade();
        OrderResumeChunkVO orderResumeChunkVO = purchaseFacade.findOrdersByDate(startDate, endDate, startIndex, count);

        if (orderResumeChunkVO.getOrderResumeVOs().size() > 0) {
            request.setAttribute("orders", orderResumeChunkVO.getOrderResumeVOs());


        } /* Generate parameters for previous and next links. */
        Map previousLinkParameters = getPreviousLinkParameters(startDate, endDate,
                startIndex, count);

        if (previousLinkParameters != null) {
            request.setAttribute("previous", previousLinkParameters);
        }

        if (orderResumeChunkVO.getExistMoreOrders()) {
            Map nextLinkParameters = getNextLinkParameters(startDate, endDate,
                    startIndex, count);
            request.setAttribute("next", nextLinkParameters);

        } /* Return ActionForward. */

        request.setAttribute("action", "FIND_BY_DATE");
        return mapping.findForward("FindOrdersByDateSuccess");

    }

    private Map getPreviousLinkParameters(Calendar startDate, Calendar endDate,
            int startIndex, int count) {

        Map linkAttributes = null;

        if ((startIndex - count) > 0) {

            linkAttributes = getCommonPreviousNextLinkParameters(startDate, endDate,
                    startIndex, count);
            linkAttributes.put("startIndex", new Integer(startIndex - count));
        }

        return linkAttributes;

    }

    private Map getNextLinkParameters(Calendar startDate, Calendar endDate,
            int startIndex, int count) {

        Map linkAttributes = getCommonPreviousNextLinkParameters(startDate, endDate,
                startIndex, count);
        linkAttributes.put("startIndex", new Integer(startIndex + count));

        return linkAttributes;

    }

    private Map getCommonPreviousNextLinkParameters(Calendar startDate, Calendar endDate,
            int startIndex, int count) {

        Map linkAttributes = new HashMap();

        linkAttributes.put("startDate", startDate);
        linkAttributes.put("endDate", endDate);
        linkAttributes.put("count", new Integer(count));

        return linkAttributes;

    }
}
